﻿<%@ WebHandler Language="C#" Class="PersonalAchievementRank" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business.BLL;

public class PersonalAchievementRank: AjaxHandlerBase
{

    //*******************************************
    //功能概要       ：基本权限初始化设定
    //参数説明       ：-
    //返回值説明     ：-
    //功能詳細       ：权限哈希表中添加操作与权限组
    //作成者         ：he.jifeng 
    //作成日         ：2010-09-27
    //*******************************************
    public PersonalAchievementRank()
    {
        //导出
        htTypePermit.Add("export", Constants.Permit_AchievementRank_Export);
        htTypePermit.Add("exportall", Constants.Permit_AchievementRank_Export);
    }

    //*******************************************
    //功能概要       ：请求处理
    //参数説明       ：context 请求上下文
    //返回值説明     ：-
    //功能詳細       ：根据请求名 分发请求
    //作成者         ：he.jifeng
    //作成日         ：2010-09-27
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //个人业绩排行
                case "search":
                    strRet = SearchRank(context);
                    break;
                //个人业绩详情列表
                case "getdetail":
                    strRet = GetDetailAchievement(context);
                    break;
                //导出全部
                case "exportall":
                    strRet = ExportAll(context);
                    break;
                default:
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private string ExportAll(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();

        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractBaseInfo bllBaseInfo = new ContractBaseInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }

        strParam = context.Request.Params["BusinessType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BusinessType", strParam);
        }
        strParam = context.Request.Params["UserCode"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("UserCode", strParam);
        }
        strParam = context.Request.Params["Status"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Status", strParam);
        }

        if (!string.IsNullOrEmpty(context.Request.Params["userid"]))
        {
            dicParams.Add("UserId", context.Request.Params["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_All))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_City))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_Company))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_Depart))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());
        }
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }

        System.Data.DataSet dsBaseInfo = bllBaseInfo.GetPersonalRankExportAll(dicParams);

        string strHTML = "";

        strHTML = GetHTML(dsBaseInfo.Tables[0]);

        strHtml.Append("{");
        strHtml.Append("success:'1',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:'{0}'", strHTML);
        strHtml.Append("}");

        strRet = strHtml.ToString();
        return strRet;
    }

    private string GetHTML(System.Data.DataTable dt)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table class=\"tabsorternew table_archive\"><tr>");
        sb.Append("<th style=\"width:40px;\">名次</th>");
        sb.Append("<th style=\"width:110px;\">员工工号</th>");
        sb.Append("<th style=\"width:110px;\">员工姓名</th>");
        sb.Append("<th style=\"width:120px;\">商圈</th>");
        sb.Append("<th style=\"width:180px;\">部门</th>");
        sb.Append("<th style=\"width:80px;\">应收佣收</th>");
        sb.Append("<th style=\"width:80px;\">业绩</th>");
        sb.Append("<th style=\"width:80px;\">点数</th>");
        sb.Append("<th style=\"width:80px;\">年资</th>");
        sb.Append("<th style=\"width:120px;\">职称</th></tr>");

        double AllCommision = 0.0;     //开单金额总计
        double PureCommision = 0.0;      //收款  
        double Point = 0.0;      //付款

        foreach (System.Data.DataRow dr in dt.Rows)
        {
            sb.Append("<tr>");
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "RANK_PURE", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "UserCode", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "Employee", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "AreaName", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "Depart", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "AllCommision", 0));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "PureCommision", 0));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "Point", 0));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "WorkAgeBegin", ""));
            sb.AppendFormat("<td>{0}</td>", DatasetHelper.GetValue(dr, "Position", ""));
            sb.Append("</tr>");

            AllCommision += string.IsNullOrEmpty(dr["AllCommision"].ToString()) ? 0 : double.Parse(dr["AllCommision"].ToString());
            PureCommision += string.IsNullOrEmpty(dr["PureCommision"].ToString()) ? 0 : double.Parse(dr["PureCommision"].ToString());
            Point += string.IsNullOrEmpty(dr["Point"].ToString()) ? 0 : double.Parse(dr["Point"].ToString()); 
        }
        sb.AppendFormat("<tr><td></td><td>总合计</td><td></td><td></td><td></td><td>{0}</td><td>{1}</td><td>{2}</td>", AllCommision, PureCommision, Point);
        sb.Append("<td></td><td></td></tr></table>");

        return sb.ToString();
    }
    

    //*******************************************
    //功能概要       ：查询个人业绩排行榜数据
    //参数説明       ：context 请求上下文
    //返回值説明     ：响应信息
    //功能詳細       ：查询个人业绩排行榜数据
    //作成者         ：he.jifeng
    //作成日         ：2010-09-27
    //*******************************************
    private string SearchRank(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();

        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractBaseInfo bllBaseInfo = new ContractBaseInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }

        strParam = context.Request.Params["BusinessType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BusinessType", strParam);
        }
        strParam = context.Request.Params["UserCode"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("UserCode", strParam);
        }
        strParam = context.Request.Params["Status"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Status", strParam);
        }

        if (!string.IsNullOrEmpty(context.Request.Params["userid"]))
        {
            dicParams.Add("UserId", context.Request.Params["userid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["departid"]))
        {
            dicParams.Add("DepartId", context.Request.Params["departid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["areaid"]))
        {
            dicParams.Add("AreaId", context.Request.Params["areaid"]);
        }
        else if (!string.IsNullOrEmpty(context.Request.Params["companyid"]))
        {
            dicParams.Add("CompanyId", context.Request.Params["companyid"]);
        }
        if (!string.IsNullOrEmpty(context.Request.Params["cityid"]))
        {
            dicParams.Add("CityId", context.Request.Params["cityid"]);
        }
        else
        {
            dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
        }

        //查看所有
        if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_All))
        {
        }
        //查看本城市
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_City))
        {
            if (!dicParams.ContainsKey("CityId"))
            {
                dicParams.Add("CityId", CurrentUserInfo.CityId.ToString());
            }
        }
        //查看本公司
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_Company))
        {
            if (!dicParams.ContainsKey("CompanyId"))
            {
                dicParams.Add("CompanyId", CurrentUserInfo.CompanyId.ToString());
            }
        }
        else if (CurrentUserInfo.ContainsPermit(Constants.Permit_PersonalRank_Depart))
        {
            dicParams.Add("CurDepartId", CurrentUserInfo.UserId.ToString());
        }            
        else
        {
            dicParams.Add("CurUserId", CurrentUserInfo.UserId.ToString());
        }

        System.Data.DataSet dsBaseInfo = bllBaseInfo.GetPersonalRankByParams(dicParams);

        strHtml.Append("{");
        strHtml.Append("success:'1',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:{{{0},", JsonHelp.Convert2Json(dsBaseInfo.Tables[0]));
        strHtml.AppendFormat("count:'{0}'", dsBaseInfo.Tables[1].Rows[0][0]);
        strHtml.Append("}");
        strHtml.Append("}");

        strRet = strHtml.ToString();
        
        return strRet;
    }

    //*******************************************
    //功能概要       ：获得个人业绩详情列表
    //参数説明       ：context 请求上下文
    //返回值説明     ：响应信息
    //功能詳細       ：获得个人业绩详情列表
    //作成者         ：he.jifeng
    //作成日         ：2010-09-28
    //*******************************************
    private string GetDetailAchievement(HttpContext context)
    {
        string strRet = string.Empty;
        string strParam = string.Empty;
        StringBuilder strHtml = new StringBuilder();

        Dictionary<string, string> dicParams = new Dictionary<string, string>();
        ContractBaseInfo bllBaseInfo = new ContractBaseInfo();

        strParam = context.Request.Params["pagesize"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageSize", strParam);
        }
        strParam = context.Request.Params["pageindex"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("PageIndex", strParam);
        }
        strParam = context.Request.Params["sort"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("Sort", strParam);
        }
        strParam = context.Request.Params["StartTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("StartTime", strParam);
        }
        strParam = context.Request.Params["EndTime"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("EndTime", strParam);
        }

        strParam = context.Request.Params["BusinessType"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("BusinessType", strParam);
        }
        //strParam = context.Request.Params["UserCode"];
        //if (!string.IsNullOrEmpty(strParam))
        //{
        //    dicParams.Add("UserCode", strParam);
        //}
        //strParam = context.Request.Params["Status"];
        //if (!string.IsNullOrEmpty(strParam))
        //{
        //    dicParams.Add("Status", strParam);
        //}

        strParam = context.Request.Params["EmployeeId"];
        if (!string.IsNullOrEmpty(strParam))
        {
            dicParams.Add("UserId", strParam);
        }

        System.Data.DataSet dsBaseInfo = bllBaseInfo.GetPersonalAchievementByParams(dicParams);

        strHtml.Append("{");
        strHtml.Append("success:'1',");
        strHtml.Append("msg:'',");
        strHtml.AppendFormat("data:{{{0},", JsonHelp.Convert2Json(dsBaseInfo.Tables[0]));
        strHtml.AppendFormat("count:'{0}'", dsBaseInfo.Tables[1].Rows[0][0]);
        strHtml.Append("}");
        strHtml.Append("}");

        strRet = strHtml.ToString();

        return strRet;
    }
}